package logic.service;

import logic.facade.SubjectFacade;
import dao.GroupDAO;
import dao.StudentDAO;
import entity.ClassGroup;
import entity.Grades;
import entity.Student;
import entity.Subject;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import vo.GradesVO;
import vo.GroupVO;
import vo.IValueObject;
import vo.SubjectVO;

/**
 *
 * @author Alexander
 */
public class GroupService implements IService {

    @Override
    public void persist(IValueObject vo, EntityManager em) {
        ClassGroup entity = new ClassGroup();
        entity.setId(((GroupVO) vo).getId());
        entity.setGroupNumber(((GroupVO) vo).getGroupNumber());
        //TODO
        //entity.setGrades(((GroupVO)vo).getGrades());
//        entity.setClassDays(((GroupVO)vo).getClassDays());

        Subject materia = new Subject();
        SubjectVO materiaVO = (SubjectVO) new SubjectFacade().find(((GroupVO) vo).getSubjectId());
        materia.setCode(materiaVO.getCode());
        materia.setName(materiaVO.getName());
        entity.setSuject(materia);

        new GroupDAO().persist(entity, em);
    }

    @Override
    public IValueObject find(Object id, EntityManager em) {
        ClassGroup g = (ClassGroup) new GroupDAO().find(id, em);
        if (g != null) {
            return g.toVO();
        } else {
            return null;
        }
    }

    @Override
    public void update(IValueObject vo, EntityManager em) {
        ClassGroup entity = new ClassGroup();
        entity.setId(((GroupVO) vo).getId());
        entity.setGroupNumber(((GroupVO) vo).getGroupNumber());
        //TODO
        //entity.setGrades(((GroupVO)vo).getGrades());
        //entity.setClassDays(((GroupVO)vo).getClassDays());

        //entity.setSuject(((GroupVO)vo).getSubjectId());

        new GroupDAO().update(entity, em);
    }

    @Override
    public void delete(Object id, EntityManager em) {
        new GroupDAO().delete(id, em);
    }

    @Override
    public List<IValueObject> getList(EntityManager em) {
        List<IValueObject> groupVO = new ArrayList<>();
        for (Object group : new GroupDAO().getList(em)) {
            groupVO.add(((ClassGroup) group).toVO());
        }
        return groupVO;
    }

}
